#include <string.h>

#include "sms.h"

static  char *filelist_get_parent_dir(FileList *fl)
{
	char *dir=fl->cur_dir;
	char *p=NULL,*q=NULL;
	char *parent_dir;
	if(strcmp("/",dir)==0)
		return NULL;
	p=dir;
	while(p=strchr(p,'/'))
	{
		if(*(p+1)!='\0')
			q=p;
		p++;
	}
	
	if(q)
	{
		parent_dir=g_strndup(dir,q-dir);
		printf("%s:[%s]\n",__FUNCTION__,parent_dir);
		return parent_dir;
	}
	else
		return NULL;
}

static gboolean filelist_release_node(ListView *lv,gpointer data)
{
	SFile *sf=data;
	if(sf)
	{
		free(sf->fullname);
		free(sf);
	}
	return TRUE;
}

static gboolean is_support_file(const char *name,char **ext)
{
	char *seek;
	while(*ext)
	{
		seek=(char *)strcasestr(name,*ext);
		if(seek)
		{	
			if(seek[strlen(*ext)]=='\0')
				return TRUE;
		}
		ext++;
	}
	return FALSE;
}

static GList * add_to_list_if_support(GList *list,const char *name,const char *fullname,int type)
{
#if 0
	if((type & STYPE_MOVIE) && is_support_file(name,global.config.file_ext.movie))
		return add_to_list(list,name,fullname,STYPE_MOVIE);
	else if((type & STYPE_MUSIC) && is_support_file(name,global.config.file_ext.music))
		return add_to_list(list,name,fullname,STYPE_MUSIC);
	else if((type & STYPE_PIC) && is_support_file(name,global.config.file_ext.pic))
		return add_to_list(list,name,fullname,STYPE_PIC);
	else if((type & STYPE_GAMES) && is_support_file(name,global.config.file_ext.games))
		return add_to_list(list,name,fullname,STYPE_GAMES);
#endif
	return NULL;
}

static GList *add_to_list(GList *list,const char *name,const char *fullname,SUPPORT_TYPE type)
{
	Node * n;
	SFile *sf =(SFile *) g_malloc0(sizeof(SFile) + 1);

	
	sf->type=type;
	sf->fullname=g_strdup(fullname);
	
	n = listview_item_create(NULL,NULL,name,sf);
	
	return g_list_append(list,n);
}

/////////////////////////////////////////////////

FileList *filelist_new(GooCanvasItem *parent,int x,int y,int icon_type,Theme *theme)
{
	FileList *fl;
	fl = (FileList *) g_malloc0(sizeof(FileList)+1);
	
	fl->lv=listview_new(parent,x,y,theme,icon_type);
	
	fl->lv->free_node_private=filelist_release_node;
	
	g_object_set(fl->lv->cavas,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
	
	return fl;
}

void filelist_destroy(FileList *fl)
{
	if(!fl)
		return;	
	if(fl->cur_dir)
		g_free(fl->cur_dir);
	
	listview_destroy(fl->lv);
}

gboolean filelist_load_dir(FileList *fl,const char *dir,int member_type)
{
	GError        *error = NULL;
	GDir          *directoryhandle;
	GList *dirs=NULL,*files=NULL,*tmp_list=NULL;
	gchar *go_dir,*entryname;
	SFile *sf;
	int i;
	
	if(!fl)
		return FALSE;
	
	if(strcmp(dir,"..")==0)
	{
		go_dir=filelist_get_parent_dir(fl);
		if(!go_dir)
			return FALSE;
	}
	else
		go_dir=strdup(dir);
	
	listview_list_init(fl->lv);	

	if(fl->cur_dir)
		free(fl->cur_dir);
	fl->cur_dir=go_dir;
	fl->type=member_type;

	directoryhandle = g_dir_open(fl->cur_dir, 0, &error);
	if (error)
	{
		g_printerr ("g_dir_open(%s) failed - %s\n", fl->cur_dir, error->message);
		g_error_free(error);
		return FALSE;
	}
	
	//add parent dir
	dirs=add_to_list(dirs,"..","..",STYPE_DIR);

	while ((entryname=(gchar *)g_dir_read_name(directoryhandle)))
	{
printf("entryname=%s\n",entryname);
		gchar *fullpath;

		if(strcmp(entryname,"lost+found")==0||entryname[0]=='.')
			continue;	
	
		fullpath = g_strconcat (go_dir, G_DIR_SEPARATOR_S, entryname, NULL);
		if(g_file_test(fullpath, G_FILE_TEST_IS_DIR))
		{
			dirs=add_to_list(dirs,entryname,fullpath,STYPE_DIR);
			g_free(fullpath);
			continue;
		}
		printf("add to support,fullpath=%s\n",fullpath);
		tmp_list=add_to_list_if_support(files,entryname,fullpath,member_type);
		if(tmp_list)
			files=tmp_list;
		g_free(fullpath);
	}
	g_dir_close(directoryhandle);
	fl->lv->items=g_list_concat(dirs,files);

	//printf("fl->files=%d,dirs=%d,files=%d\n",g_list_length(fl->files),g_list_length(dirs),g_list_length(files));
printf("aaa\n");
	listview_translate(fl->lv,0-fl->lv->view_cur);
	printf("bbb\n");
	fl->lv->list_count=g_list_length(fl->lv->items);
	listview_show(fl->lv);
	printf("ccc\n");
	return TRUE;
}

SFile * filelist_get_cur(FileList *fl)
{
	ListView *lv=fl->lv;
	Node * node;
	int id=lv->view_cur+lv->list_cur;
	
	node=g_list_nth_data(lv->items,id);
	return node->private;
}
